package com.weixinadmin.dao;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;   
import java.sql.PreparedStatement;   
import java.sql.ResultSet;   
import java.sql.SQLException;   
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;   
  
import javax.sql.DataSource;   

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;   
import org.springframework.jdbc.core.PreparedStatementCreator;   
import org.springframework.jdbc.core.RowMapper;   
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.jdbc.support.GeneratedKeyHolder;   
import org.springframework.jdbc.support.KeyHolder;   

import java.security.*;

public class UserSql {
	  private static Logger logger = Logger.getLogger(UserSql.class);//log4j
	
	  private JdbcTemplate jdbcTemplate;   
      //private NamedParameterJdbcTemplate namedParameterJdbcTemplate;   
	  //private SimpleJdbcTemplate simpleJdbcTemplate;   
	  private Userdao ud;
	  
	class userRowMapper implements RowMapper {  
		        
		public Object mapRow(ResultSet rs, int index) throws SQLException {  
			Userdao ud = new Userdao();
			ud.setpassword(rs.getString("password"));      		      
			ud.setname(rs.getString("name"));    		      		      
		return ud;         	      
		}         
		      
	 }  
	 public UserSql(){
		 this.jdbcTemplate = new JdbcManager().getjdbctemplate();
	 } 	  
	  
	  public List<Userdao> getuser() {

		  // TODO Auto-generated method stub
		  if(null==jdbcTemplate){
			  logger.info("jdbcTemplate is null");
		  }
          return jdbcTemplate .query( "select * from user" ,
        		  new userRowMapper());

	  } 
	  
	  public Userdao getuser(String name) {

          // TODO Auto-generated method stub
		  if(null==jdbcTemplate){
			  logger.info("jdbcTemplate is null");
		  }
          return (Userdao) jdbcTemplate.queryForObject(
        		  "select * from user where id=?" , new Object[] { name }, 
                   new int [] { java.sql.Types. INTEGER }, new userRowMapper());

	  }
	  
	  public void add(Userdao user){

		  	if(null==jdbcTemplate){
				  logger.info("jdbcTemplate is null");
				  return ;
			}
			try { 
				  jdbcTemplate.update("INSERT INTO user (name,password) VALUES(?,?)",
						  new Object[]{user.getname(),user.getpassword()});
			} catch (Exception e) {  
				  e.printStackTrace();  
			}  
	  }
	    
	  public void del(){	
		//  return jdbcTemplate.update("delete * from custmergroup", new Object[]{});
		  
		  if(null==jdbcTemplate){
			  logger.info("jdbcTemplate is null");
			  return ;
		  }
		  try { 
			  jdbcTemplate.update("delete * from user",
					  new Object[]{});
		  }	catch (Exception e) {  
			  	e.printStackTrace();  
		  } 
	  
	  }
	 
		
	  
	  public static void main(String[] args) throws UnsupportedEncodingException, NoSuchAlgorithmException  {
		  
		  UserSql us = new UserSql();
		//  cgs.setDataSource(dataSource);
		  MessageDigest md5 = MessageDigest.getInstance("MD5");
		  md5.digest("admin".getBytes()); 
		  
		  Userdao user = new Userdao();
		 user.setname("admin");
		 String passwd = new String( md5.digest("admin".getBytes()),"utf-8");
		 user.setpassword(passwd);
		 
		  us.add(user);
		  System.out.println("add: " + user.toString());
		  List<Userdao> uss =  us.getuser();
		  
		  Iterator<Userdao> itr = uss.iterator();
	        while (itr.hasNext()) {
	        	user = itr.next();
	        	System.out.println("query: " + user.toString());
	    		 
	        }
         
  } 
}



